<h1>Report an Issue</h1>
<?
$emailContent = "";
$emailBody = "";

if (isset($_POST)) {
	
	$sql = "INSERT INTO er_reported_problems (comment) values ('temp')";
	r_db($sql);
	
	$sql = "SELECT LAST_INSERT_ID()";
	$result = r_db($sql);
	$row = mysql_fetch_array($result);
	
	$sql = "UPDATE er_reported_problems SET ";
	
	foreach ($_POST as $key => $value) {
		if ($key == "dateofvisit") {
			if (strlen($value) > 0) {
				$dateofvisit=explode("/",$value);
				$day=$dateofvisit[0];
				if (isset($dateofvisit[1])) {
					$month=$dateofvisit[1];
				}
				else {
					$month = null;
				}
				if (isset($dateofvisit[2])) {
					$year=$dateofvisit[2];
				}
				else {
					$year = null;
				}
				$value=$year."-".$month."-".$day;
			}
		}
		if ($key != "sitename" && $key != "clientidtypename") {
			$sql .= "$key = '$value', ";
		}
		if ($key != "clientidtype" && $key != "site") {
			$emailContent .= "<b>" . key_extract($key) . "</b> - $value<br />";
		}
	}
	$sql = substr($sql,0,strlen($sql)-2);
	$sql .= " WHERE ID = ".$row[0];
	/*echo $sql;
	exit();*/
	$result = r_db($sql);

// Email code begin	
	$emailBody = "<style>body {font-size:10pt;font-family:arial, sans-serif;}</style><p>The following was submitted to the ERS at ".date('l dS \of F Y h:i:s A')."</p><p>$emailContent</p>";

	require("./inc/phpmailer/class.phpmailer.php");
	$mail = new PHPMailer();
	
	//$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Mailer = "mail";
	$mail->IsHTML(true); // telling the class the email is in HTML format
	$mail->Host = $smtp_email_server; // SMTP server
	$mail->From = $contact_email_address;
	$mail->FromName = "ER System";
	$mail->AddAddress("$contact_email_address");
	$mail->Subject = "ERS contact from ".$_POST['reportername'];
	$mail->Body = $emailBody;
	//$mail->WordWrap = 50;
	
	if(!$mail->Send())
	{
	   echo "<p>";
	   echo "Error - email could not be sent to administrators.<br />";
	   echo "Mailer Error: " . $mail->ErrorInfo;
	   echo "</p>";
	}
// Email code end

	//echo "<p>$sql</p>";
	//echo "<p>$emailBody</p>";

	if ($result) {
		echo "<p>Thankyou, your communication has been sent to the ERS Administrators.</p>";
	}
}

?>